package yxy.game.pm2.command.mail;

import yxy.apple.util.redisson.RedisException;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.MailMessage;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.mail.MailHandle;
import yxy.game.pm2.module.mail.MailTypeEnum;

import java.util.Collection;

/** 邮件 邮件一键领取 */
@MessageType(proto = Command.MailReceivedAll, classtype = MailMessage.MailReceivedAllRequest.class)
public class CMD_MailReceivedAll extends Command<MailMessage.MailReceivedAllRequest> {

	@Override
	public void execute(MessageSource messageSource, MailMessage.MailReceivedAllRequest request) throws RedisException {
		String playerCode = messageSource.getTag();
		MailTypeEnum type = MailTypeEnum.forNumber(request.getType().getNumber());

		MailMessage.MailReceivedAllResponse.Builder response = MailMessage.MailReceivedAllResponse.newBuilder();

		MailHandle mailHandle = new MailHandle();
		Collection<Currency> rewards = mailHandle.rewardMails(playerCode, type);

		response.addAllRewards(new CurrencyHandle().packItems(rewards));
		response.addAllMails(mailHandle.packMails(playerCode));

		send(messageSource, response, Result.Success);
	}

	private void send(MessageSource messageSource, MailMessage.MailReceivedAllResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}